const { ipcMain, BrowserWindow } = require("electron");
const path = require("path");
const winState = require("electron-win-state").default;
const saveas = require("./saveas");
let WIN = null;
const WinState = new winState({
    width: 1200,
    height: 800,
    electronStoreOptions: {
        name: "window-state-open"
    }
});
const DivBtnDomStyle = `position: fixed;bottom: 20px;right: 10px;width:100px;height:60px;line-height:60px;background-color:cornflowerblue;text-align:center;border-radius:6px;color:#FFF;cursor: pointer;z-index: 9999999999`;
const JS_DOM = `
const DivBtnDom = document.createElement("div");
DivBtnDom.id="readit-button";
DivBtnDom.innerHTML="关闭窗口";
DivBtnDom.style="${DivBtnDomStyle}";
DivBtnDom.addEventListener("click",()=> {OnNewWindowEvent.closeNewWindowEvent()})
document.body.appendChild(DivBtnDom);`;
/** 打开网址窗口 */
const openWebsite = (e, url) => {

    WIN = new BrowserWindow({
        ...WinState.winOptions,
        show: false,
        webPreferences: {
            preload: path.resolve(__dirname, "../preload/openWindowPreload.js")
        }
    });
    WIN.on("ready-to-show", () => {
        WIN.show();
    });
    WinState.manage(WIN);
    WIN.loadURL(url);
    WIN.webContents.openDevTools();
    WIN.webContents.executeJavaScript(JS_DOM).catch(e => { });
    WIN.webContents.on("context-menu", (e, args) => {
        saveas(args.srcURL);
    });
};
const close = () => {
    WIN.close();
}
ipcMain.handle("on-openWebsite-event", openWebsite);

ipcMain.handle("on-close-new-window-event", close);